App({
   globalData: {
      apiBaseUrl: "https://qapi.openally.com/api_qzg/",
      apiUrl: "https://qapi.openally.com/api/public/",
      shopInfo: null,
      tableCateArr: null,
      res: null,
      isHide: [true, true],
      foodOrderList: null,
      loginMode: 'guest',
      guestInfo: '',
      wssIsInit: 0,
      isLoginWss: 0,
      isWsSender: 0//websocket发送者，1是，0否
   },

   data: {
      res: null,
      isHide: [true, true]
   },

   readyWebsocket: function (callback) {
      var tg = this.globalData
      var t = this
      console.log('【全局变量】', tg)
      if (tg.wssIsInit) {
         t.loginWebsocket('logout')
         tg.isLoginWss = 0
         // var json = {
         //    type: 'logout',
         //    name: tg.guestInfo.user,
         //    client_name: tg.guestInfo.name,
         //    room_id: tg.shopInfo.shopInfo.userid
         // }
         // var msg = JSON.stringify(json)
         // t.sendSocketMsg(msg)
      } else {
         console.log('开始首次连接wss')
         t.initWebsocket(callback)
      }
   },

   initWebsocket: function (callback) {
      var that = this
      wx.connectSocket({
         url: 'wss://wss.openally.com',
         success: function (res) {
            console.log('wx.connectSocket连接成功')
            that.loginWebsocket('login')
            if (callback) callback('initWebsocket_ok')
         },
         fail: function (res) {
            console.log('wx.connectSocket连接失败')
         },
      })

      wx.onSocketClose(function (res) {
         console.log('#############wx.onSocketClose关闭了', res)
         //that.initWebsocket()
      })
   },

   loginWebsocket: function (logType) {
      var that = this
      var t_g = this.globalData
      console.log('全局变量t_g=', t_g)
      wx.onSocketOpen(function (res) {
         console.log('wx.onSocketOpen成功')
         var json = {
            type: logType,
            name: t_g.guestInfo.user,
            client_name: t_g.guestInfo.name,
            room_id: t_g.shopInfo.shopInfo.userid
         }
         var msg = JSON.stringify(json)
         that.sendSocketMsg(msg)
      })
   },

   sendSocketMsg: function (msg) {
      wx.sendSocketMessage({
         data: msg,
         success: function (res) {
            console.log('sendSocketMsg:发送成功')
         },
         fail: function (res) {
            console.log('sendSocketMsg:发送失败')
         },
      })
   },

   playMp3: function (state) {//有新消息声音提醒
      var audio = state == 2 ? 'new_order.wav' : 'new_msg.mp3'//'notify.ogg'
      wx.playBackgroundAudio({
         dataUrl: 'http://img.openally.com/images/' + audio,
         success: function (res) {
            console.log('play success: ', res)
         },
         fail: function (res) {
            console.log('play fail: ', res)
         },
      })
   },
   //商品分类变更通知(后台)
   setNoticeGoodsCatePage: function (notice) {
      this.notice = notice
   },
   noticeUpdateGoodsCate: function (data) {
      if (this.notice) {
         this.notice(data)
      }
   },
   //某餐桌详情变更通知(前台)
   setNoticeTableList: function (notice) {
      this.tableListNotice = notice
   },
   noticeUpdateTableList: function (data) {
      if (this.tableListNotice) {
         this.tableListNotice(data)
      }
   },
   //某餐桌详情变更通知(后台)
   setNoticeTableListAdmin: function (notice) {
      this.tableListAdminNotice = notice
   },
   noticeUpdateTableListAdmin: function (data) {
      if (this.tableListAdminNotice) 
         this.tableListAdminNotice(data)

      if (this.tableListNotice) 
         this.tableListNotice('reload')
   },
   //某餐桌分类变更通知(后台)
   setNoticeTableCateList: function (notice) {
      this.tableCateNotice = notice
   },
   noticeUpdateTableCateList: function (data) {
      console.log(data)
      console.log('data')
      if (this.tableCateNotice && data.notice != 'del') //如删除不用通知
         this.tableCateNotice(data)

      if (this.tableListNotice)
         this.tableListNotice(data)
   },
   //服务员增改通知(后台)
   setNoticeWaiterList: function (noti) {
      this.waiterNotice = noti
   },
   noticeUpdateWaiterList: function (data) {
      if (this.waiterNotice)
         this.waiterNotice(data)
   },
   //顾客点餐后通知生成小票
   setNoticeTicket: function (noti) {
      this.ticketNotice = noti
   },
   noticeTicket: function (noti) {
      if (this.ticketNotice)
         this.ticketNotice(noti)
   },

   //編辑商品后的通知更新
   setNoticetGoodsList: function (noti) {
      this.goodsListNotice = noti
   },
   noticeGoodsList: function (noti) {
      if (this.goodsListNotice)
         this.goodsListNotice(noti)
   },

   onLaunch: function () { },

   getJsonData: function (urlTail, param, callback) {
      var aipUrl = this.globalData.apiBaseUrl + urlTail
      this.request(aipUrl, param, callback)
   },

   getNetData: function (urlTail, param, callback) {
      var aipUrl = this.globalData.apiUrl + urlTail
      this.request(aipUrl, param, callback)
   },

   request: function (aipUrl, param, callback) {
      var that = this
      wx.request({
         url: aipUrl,
         data: param,
         method: 'POST',
         success: function (res) {
            console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【成功】', res);
            var d = res.data
            if (d.code == 0 || d.code == 92900 || d.code == 81) {
               callback(res.data)
            } else that.toast(d.message)
         },
         fail: function (res) {
            console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【失败】', res);
         },
      })
   },

   toast: function (msg) {
      wx.showToast({
         title: msg,
         image: '/pages/images/qzg_logo_90x.jpg',
      })
   },

   modal: function (msg, title) {
      wx.showModal({
         title: title ? title : '温馨提示',
         content: msg,
      })
   }
})
